#include <Servo.h> 

//风扇
#define IN11 42
#define IN12 44

//手部左右移动及旋转
#define IN31 46
#define IN32 48
#define IN33 50
#define IN34 52
#define EN3A 2
#define EN3B 3

//洗发液的开放
#define IN41 39   
#define IN42 41
#define IN43 43
#define IN44 45

//发梢清洗及手部前后运动
#define IN21 47
#define IN22 49
#define IN23 51
#define IN24 53

//舵机的初始化
Servo servo_1; // 左手大臂
Servo servo_2;  //左手小臂
Servo servo_3;  //右手大臂
Servo servo_4;  //右手小臂

Servo servo_5;  //额头清洗1
Servo servo_6;  //额头清洗2


int pos = 0;    //循环变量 （角度）

void setup() {
  //引脚初始化

  pinMode(IN11,OUTPUT);
  pinMode(IN12,OUTPUT);

  pinMode(IN21,OUTPUT);
  pinMode(IN22,OUTPUT);
  pinMode(IN23,OUTPUT);
  pinMode(IN24,OUTPUT);

  pinMode(IN31,OUTPUT);
  pinMode(IN32,OUTPUT);
  pinMode(IN33,OUTPUT);
  pinMode(IN34,OUTPUT);

  //初始化低电平
  digitalWrite(IN11,LOW);
  digitalWrite(IN12,LOW);

  digitalWrite(IN21,LOW);
  digitalWrite(IN22,LOW);
  digitalWrite(IN23,LOW);
  digitalWrite(IN24,LOW);

  digitalWrite(IN31,LOW);
  digitalWrite(IN32,LOW);
  digitalWrite(IN33,LOW);
  digitalWrite(IN34,LOW);

  //舵机引脚设置
  servo_1.attach(4); // 配置myservo实例的引脚为6
  servo_2.attach(5); // 配置myservo实例的引脚为9
  servo_3.attach(6); // 配置myservo实例的引脚为10
  servo_4.attach(7); // 配置myservo实例的引脚为11
  servo_5.attach(11); // 配置myservo实例的引脚为10
  servo_6.attach(12); // 配置myservo实例的引脚为11

  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial3.begin(9600);
}

void loop() {
  if(Serial.available()||Serial3.available()){
    delay(100);
    char buffer;
    buffer=char(Serial.read());

    int val = 0;
    val = Serial3.read();//读出串数据

    //两根螺杆开始循环
    if(buffer=='1'){
      Serial.println(buffer);
      Serial1.print(buffer);
    }

    //两个螺杆结束循环
    else if(buffer=='2'){
      Serial.println(buffer);
      Serial1.print(buffer);
    }

    //开始按摩
    else if((buffer=='3')||(val==3)){
      Serial.println("3");
      Serial2.print("3");

      //螺杆上升
      //Serial1.print("3");

    }
    //结束按摩
    else if((buffer=='4')||(val==4)){
      Serial.println("4");
      Serial2.print("4");

      //螺杆下降
      //Serial1.print("4");
    }

    //开始擦头发
    else if((buffer=='5')||(val==5)){
      Serial.println("5");
      Serial2.print("5");
    }
    
    //结束擦头发
    else if((buffer=='6')||(val==6)){
      Serial.println(buffer);
      Serial2.print("6");
    }

    //装置开始洗头
    else if((buffer=='8')||(val == 8)){
      Serial.println(buffer);
      Serial1.println('5');
      delay(10000);
      All1();
    }

    else if(buffer=='9'){
      Serial.println(buffer);
      Fan();
      }
    else if(buffer=='0'){
      Serial.println(buffer);
      End();
      }
  }
}

void All1(){
  qianhou1();
  Initial();
  End();
  Serial1.print('1');
  digitalWrite(IN21,HIGH);

  //开始搓
  Rubbing_0();
  Rubbing2();
  Rubbing1();
  Rubbing2();
  Rubbing1();
  Rubbing2();
  Rubbing1();

  //喷洗发液
  xifaye();
  Rubbing2();
  Rubbing1();
  Rubbing2();
  Rubbing1();
  Rubbing2();
  Rubbing1();

  Rubbing_1();

  digitalWrite(IN21,LOW);
  qianhou2();
  Initial();
  Serial1.println('2');

  Serial2.println('5');
  delay(30000);
  
  Fan();

  /*
  while(1){
    Rubbing2();
    Rubbing1();

    char buffer1;
    buffer1=char(Serial.read());
    if(buffer1=='2'){
      Serial1.print('2');
      break;
    }
    if(buffer1=='3'){
      Serial2.print('3');
      break;
    }
    if(buffer1=='4'){
      Serial2.print('4');
      break;
    }
  }
  */
  
}

//喷洗发液的代码
void xifaye(){
  //喷洗发液
  digitalWrite(IN43,HIGH);
  delay(4000);
  digitalWrite(IN43,LOW);
  digitalWrite(IN41,HIGH);
  delay(5000);
  digitalWrite(IN41,LOW);
  }

//手部螺杆前后移动
//往后移动
void qianhou2(){
  digitalWrite(IN24,HIGH);
  delay(6000);
  digitalWrite(IN24,LOW);
  }
//往前移动
void qianhou1(){
  digitalWrite(IN23,HIGH);
  delay(6000);
  digitalWrite(IN23,LOW);
  }

//手部代码
void Rubbing_0(){
  digitalWrite(IN31,HIGH);
  digitalWrite(IN34,HIGH);
  analogWrite(EN3A,240);     //圆周A方向运动
  analogWrite(EN3B,255);     //直线A方向运动
  ca();
  analogWrite(EN3B,0);
  ca();
  delay(2000);
  analogWrite(EN3A,0);
  digitalWrite(IN31,LOW);
  digitalWrite(IN34,LOW);
  delay(500);
  ca();
  }

void Rubbing_1(){
  digitalWrite(IN32,HIGH);
  digitalWrite(IN33,HIGH);
  analogWrite(EN3A,240);     //圆周A方向运动
  analogWrite(EN3B,255);
  ca();     //直线A方向运动
  analogWrite(EN3B,0);
  ca();
  analogWrite(EN3A,0);
  digitalWrite(IN32,LOW);
  digitalWrite(IN33,LOW);

  //发梢停止清洗
  delay(500);
  ca();
  }

void Rubbing1(){
  //圆周和直线运动
  digitalWrite(IN31,HIGH);
  digitalWrite(IN34,HIGH);
  analogWrite(EN3A,255);     //圆周A方向运动
  analogWrite(EN3B,255); 
  ca();    //直线A方向运动
  delay(3000);
  analogWrite(EN3B,0);
  delay(1000);
  ca();
  analogWrite(EN3A,0);
  digitalWrite(IN31,LOW);
  digitalWrite(IN34,LOW);
  delay(500);
  ca();
  }
  
void Rubbing2(){
  digitalWrite(IN32,HIGH);
  digitalWrite(IN33,HIGH);
  analogWrite(EN3A,210);     //圆周A方向运动
  analogWrite(EN3B,255);     //直线A方向运动
  ca();
  delay(3000);
  analogWrite(EN3B,0);
  ca();
  analogWrite(EN3A,0);
  digitalWrite(IN32,LOW);
  digitalWrite(IN33,LOW);
  delay(500);
  ca();
  }

//手部起始位置
void Initial(){
  servo_1.write(90);
  delay(1000);
  servo_2.write(90);
  delay(1000);
  servo_3.write(90);
  delay(1000);
  servo_4.write(90);
  delay(1000);
}

//手部结束位置
void End(){
  servo_1.write(110);
  delay(1000);
  servo_2.write(110);
  delay(1000);
  servo_3.write(60);
  delay(1000);
  servo_4.write(60);
  delay(1000);
}

void ca(){
  servo_1.write(90);
  servo_4.write(90);
  servo_5.write(90);
  servo_6.write(90);
  delay(500);
  servo_1.write(135);
  servo_4.write(45);
  servo_5.write(45);
  servo_6.write(45);
  
}

void Fan(){
  digitalWrite(IN11,HIGH);
  delay(30000);
  digitalWrite(IN11,LOW);
}